package com.imooc.oauth2.server.service;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.imooc.commons.model.domain.SignInIdentity;
import com.imooc.commons.model.pojo.Diners;
import com.imooc.commons.utils.AssertUtil;
import com.imooc.oauth2.server.mapper.DinersMapper;

/**
 * \* @author: bear
 * \* Date: 2021-07-11-15:26
 * \* describe:
/**
 * 登录校验
 */
@Service
public class UserService implements UserDetailsService {

	@Resource
	private DinersMapper dinersMapper;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		AssertUtil.isNotEmpty(username,"用户名不能为空");
		Diners diners = dinersMapper.selectByAccountInfo(username);
		if(diners == null){
			throw new UsernameNotFoundException("用户名或者密码不正确，请重新输入");
		}
		SignInIdentity signInIdentity = new SignInIdentity();
		BeanUtils.copyProperties(diners,signInIdentity);
		return signInIdentity;
	}
}
